package com.tml.security;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

public class MsgDigest {

	public static void main(String[] args) throws NoSuchAlgorithmException {
		String msg = "Hello World!";
		
		MessageDigest MD5Digest = MessageDigest.getInstance("MD5");
		// 更新要计算的内容
		MD5Digest.update(msg.getBytes());
		// 完成哈希计算，得到摘要
		byte[] md5Encode = MD5Digest.digest();
		
		MessageDigest shaDigest = MessageDigest.getInstance("SHA");
		// 更新要计算的内容
		shaDigest.update(msg.getBytes());
		// 完成哈希计算，得到摘要
		byte[] shaEncode = shaDigest.digest();
		
		System.out.println("原文：" + msg);
		System.out.println("MD5摘要：" + Base64.getUrlEncoder().encodeToString(md5Encode));
		System.out.println("SHA摘要：" + Base64.getUrlEncoder().encodeToString(shaEncode));
	}
}
